package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.constant.UrlConstant;
import com.spider.gp.dao.GpInfo;
import com.spider.gp.dao.GpInfoDao;
import com.spider.gp.dao.ZhiShu;
import com.spider.gp.dao.ZhiShuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * description:
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class GpInfoProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private GpInfoDao gpInfoDao;


    @Override
    public void process(Page page) {
        List<String> urlList = new ArrayList<String>();

        if (page.getUrl().toString().equals(UrlConstant.DFCF_GG_ALL_INFO)) {
            List<String> all = page.getJson().jsonPath("$.data.diff").all();
            all.parallelStream().forEach(item ->   {
                HashMap hashMap = JSON.parseObject(item, HashMap.class);
                //f9,f12,f13,f14,f20,f21,f115
                String code  = hashMap.get("f12").toString();
                String type = hashMap.get("f13").toString();
                String name = hashMap.get("f14").toString();
                String zsz = "0";
                String ltsz = "0";
                try {
                    zsz = hashMap.get("f20").toString().contains("-") ?  hashMap.get("f20").toString() : new BigDecimal(hashMap.get("f20").toString()).divide(new BigDecimal(100000000)).setScale(2 ,BigDecimal.ROUND_HALF_UP).toString() + "亿"; //流通市值 hashMap.get("ltsz").toString();
                }catch(Exception e){
                    System.out.println(code +"zsz报错了");
                }

                try {
                    ltsz =  hashMap.get("f21").toString().contains("-") ?  hashMap.get("f21").toString() :  new BigDecimal(hashMap.get("f21").toString()).divide(new BigDecimal(100000000)).setScale(2 ,BigDecimal.ROUND_HALF_UP).toString() + "亿"; //流通市值 hashMap.get("ltsz").toString();
                }catch(Exception e){
                    System.out.println(code +"ltsz报错了");
                }
                String dtsyl = hashMap.get("f9").toString();
                int i = gpInfoDao.updateById(GpInfo.builder().code(code).shortName(name).securityCodeType(type).zsz(zsz).ltsz(ltsz).dtsyl(dtsyl).build());
                if (i == 0) {
                   gpInfoDao.insert(GpInfo.builder().code(code).shortName(name).securityCodeType(type).zsz(zsz).ltsz(ltsz).dtsyl(dtsyl).build());
                }
                String url = UrlConstant.DFCF_GG_INFO + "(SECURITY_CODE%3D%22"+code+"%22)";
                urlList.add(url);
            });

            page.addTargetRequests(urlList);
        }else {
            System.out.println("基础信息爬虫开始了！");
            List<String> all = page.getJson().jsonPath("$.result.data").all();
            String item = all.get(0);
            HashMap hashMap = JSON.parseObject(item, HashMap.class);

            String code = hashMap.get("SECURITY_CODE").toString(); //编码
            String secucode = hashMap.get("SECUCODE").toString(); //编码全
            String orgName = hashMap.get("ORG_NAME").toString(); //全称
            String formerName = hashMap.get("FORMERNAME") == null ? "": hashMap.get("FORMERNAME").toString(); //历史用名
            String regionbk = hashMap.get("REGIONBK").toString(); //省份
            String bk = hashMap.get("EM2016") == null ? "" : hashMap.get("EM2016").toString(); //板块
            String blgainian = hashMap.get("BLGAINIAN") == null ? "": hashMap.get("BLGAINIAN").toString(); //概念板块
            String mainBusiness = hashMap.get("MAIN_BUSINESS") == null ? "" : hashMap.get("MAIN_BUSINESS").toString(); //主营业务
            String totalNum = hashMap.get("TOTAL_NUM") == null ? "" : hashMap.get("TOTAL_NUM").toString(); //员工人数
            String listingDate =  hashMap.get("LISTING_DATE") == null ? "" : hashMap.get("LISTING_DATE").toString(); //上市时间
            String chairman =  hashMap.get("CHAIRMAN") == null ? "" : hashMap.get("CHAIRMAN").toString(); //董事长
            String orgProfie = hashMap.get("ORG_PROFIE") == null ? "" :hashMap.get("ORG_PROFIE").toString(); //简要介绍
            String orgWeb = hashMap.get("ORG_WEB") == null ? "" : hashMap.get("ORG_WEB").toString(); //官网

            int i = gpInfoDao.updateById(GpInfo.builder().code(code).secucode(secucode).orgName(orgName).formerName(formerName).regionbk(regionbk).bk(bk)
                    .blgainian(blgainian).mainBusiness(mainBusiness).totalNum(totalNum).listingDate(listingDate).chairman(chairman).orgProfie(orgProfie).orgWeb(orgWeb).build());

        }





    }

    @Override
    public Site getSite() {
        return site;
    }
}








